package com.mydomain.debtbook;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class ListGroupActivity extends ListActivity {

	protected DebtAdapter myDBAdapter;
	private SimpleCursorAdapter cursorAdapter;
	private Cursor cursor;
	private Button submit;
	private Button back;
	private OnClickListener myClickListener;
	private ListView listView;
	private ArrayList<String> groupList;
	private HashMap<String, HashMap<String, String>> groupListWeight;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_view_contacts);
		myDBAdapter = ((debtBookApplication) this.getApplication())
				.getDatabaseAdapter();

		submit = (Button) findViewById(R.id.lvc_submitButton);
		back = (Button) findViewById(R.id.lvc_backButton);
		groupList = new ArrayList<String>();
		groupListWeight = new HashMap<String, HashMap<String, String>>();
		
		
		myClickListener = new OnClickListener() {
			@Override
			public void onClick(View v) {

				if (v == submit) {
					submit();
				} else if (v == back) {
					back();
				}

			}
		};

		submit.setOnClickListener(myClickListener);
		back.setOnClickListener(myClickListener);
		cursor = getGroups();
		showGroups(cursor);

		listView = getListView();
		listView.setItemsCanFocus(false);
		listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

	}

	private void submit() {

		Cursor cursor;
		String name;
		SparseBooleanArray selectedItems = listView.getCheckedItemPositions();
		int size = selectedItems.size();
		for (int sbaKey = 0; sbaKey < size; sbaKey++) {

			if (selectedItems.valueAt(sbaKey)) {
				cursor = (Cursor) listView.getItemAtPosition(selectedItems
						.keyAt(sbaKey));

				name = cursor.getString(cursor
						.getColumnIndexOrThrow(DebtAdapter._GROUP));

				HashMap<String, String> mp = new HashMap<String, String>();
				groupListWeight.put(name, mp);
				groupList.add(name);

			}

		}

		Intent returnIntent = new Intent();
		returnIntent.putExtra("GroupList", groupList);
		returnIntent.putExtra("GroupListWeight", groupListWeight);

		if (getParent().getParent() == null) {
			setResult(RESULT_OK, returnIntent);
		} else {

			getParent().getParent().setResult(RESULT_OK, returnIntent);
		}

		finish();

	}

	private void back() {
		finish();
	}

	private Cursor getGroups() {
		Cursor myCursor = myDBAdapter.getAllGroup();
		if (myCursor != null) {
			startManagingCursor(myCursor);
		}
		return myCursor;
	}

	private void showGroups(Cursor cursor) {
		cursorAdapter = new SimpleCursorAdapter(this,
				android.R.layout.simple_list_item_multiple_choice, cursor,
				new String[] { DebtAdapter._GROUP },
				new int[] { android.R.id.text1 });
		this.setListAdapter(cursorAdapter);
	}

}
